<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <input type="text" placeholder="用户名" v-model="user.username">
    <input type="password" placeholder="密码" v-model="user.password">
    <input type="button" value="登录" @click="login()">
</div>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<!--引入axios框架文件-->
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>

<script>
    let v = new Vue({
        el: "body>div",
        data: {
            user: {username: "", password: ""}
        },
        methods: {
            login() {
                //发出异步请求
                axios.post("/login", v.user).then(function (response) {
                    if (response.data.code == 1) {
                        //alert("登录成功!欢迎"+response.data.data.nickname);
                        //把当前登录的用户信息保存到localStorage
                        //localStorage只能保存字符串类型,此时服务器响应回来的是CustomUserDetails对象
                        //需要将对象转成JSON格式的字符串
                        localStorage.setItem("user", JSON.stringify(response.data.data));
                        //跳转到首页
                        location.href = "/";

                    } else if (response.data.code == 1002) {
                        alert("密码错误!");
                    } else {
                        alert("用户名不存在!");
                    }
                })
            }
        }
    })
</script>
</body>
</html>